package system.service;

import com.jfinal.aop.Duang;
import com.jfinal.plugin.activerecord.Record;
import core.exception.AccountingException;
import system.dao.AccountSubjectDao;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/5/18.
 */
public class AccountSubjectService {
    AccountSubjectDao accountSubjectDao = Duang.duang(AccountSubjectDao.class);


    public Map getAccountSubjectMap(){
        Map accountSubjectMap = new LinkedHashMap();
        List<String> subject_type_list = accountSubjectDao.getType();
        for (String subject_type : subject_type_list) {
            List listByType = accountSubjectDao.getListByType(subject_type);
            accountSubjectMap.put(subject_type, listByType);
        }

        return accountSubjectMap;
    }

    /**
     * 插入会计科目
     * @param accountSubjects
     */
    public void insertAccountSubject(Map ...accountSubjects) {
        for(Map accountSubject : accountSubjects){
            Record record = new Record();
            record.setColumns(accountSubject);
            boolean exist = accountSubjectDao.isExistSubjectIdOrSubjectName(record);
            if (exist) throw new AccountingException("会计科目编号或名称已存在");
            boolean success = accountSubjectDao.insert(record);
            if (!success) throw new AccountingException("添加会计科目失败");
        }
    }

    public void updateAccountSubject(Map ...accountSubjects) {
        for(Map accountSubject : accountSubjects){
            Record record = new Record();
            record.setColumns(accountSubject);
            boolean success = accountSubjectDao.update(record);
            if (!success) throw new AccountingException("更新会计科目失败");
        }
    }
}
